package com.itheima.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class UploadController {

    /**
     *
     * @param uploadFile  代表上传的文件内容
     * @return
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile,HttpServletRequest request){
        System.out.println("这里是用来完成文件上传操作的");

        String originalFilename = uploadFile.getOriginalFilename();

        System.out.println("截取之后的文件名:"+originalFilename);
        // 还记得怎么获取  真实发布路径
        String realPath = request.getSession().getServletContext().getRealPath("/upload");
        System.out.println(realPath);
        //文件有了  uploadFile  你想把这个文件放在哪里？
        File file = new File(realPath+"/"+System.currentTimeMillis()+"_"+originalFilename);

        // 需要 把 uploadFile内容写到 file中
        // 要完成文件复制  五步
        // 有工具类一步搞定！
        try {
            uploadFile.transferTo(file);
            return "sucess";
        } catch (IOException e) {
            e.printStackTrace();


        }

       return "fail";
    }
}
